package com.ruoyi.file.utils;

/**
 * convert file to pdf.
 *
 * @author william.zhao
 * @version 1.0
 * @date 2024/06/07/ 16:12
 * @since jdk-17.0.2
 */

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * 通过poi进行转换
 */
public class CscpFileToPdfConverter {
    private static final Logger log = LoggerFactory.getLogger(CscpFileToPdfConverter.class);

    /**
     * windows operation system.
     * convert word to pdf.
     *
     * @param wordPath the word file absolute path.
     * @param pdfPath  the converted pdf file absolute path.
     */
    public static void converterToPdf(String wordPath, String pdfPath) {
        try {
            InputStream inputStream = new FileInputStream(wordPath);
            OutputStream outputStream = new FileOutputStream(pdfPath);
            IConverter converter = LocalConverter.builder().build();

            if (wordPath.contains(".doc")) {
                converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            } else {
                converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            }

            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * convert a linux file(doc/docx/ppt/pptx/xls/xlsx/txt) to pdf.
     *
     * @param filePath the file absolute path.
     * @param pdfPath  the converted pdf file.
     */
    public static String convertLinuxFileToPdf(String filePath, String pdfPath) {
        String command = "libreoffice --headless --convert-to pdf " + filePath + " --outdir " + pdfPath;
        log.info("command:-->{}", command);
        ProcessBuilder processBuilder = new ProcessBuilder(
                "/bin/bash", "-c", "export DISPLAY=:0; " + command
        );
        processBuilder.redirectErrorStream(true);

        try {
            Process process = processBuilder.start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                // log.info("cmd-->{}", line);
            }
            int exitCode = process.waitFor();
            if (exitCode != 0) {
                throw new RuntimeException("Command failed with exit code: " + exitCode);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            // if any exceptions happened, just return null.
            return null;
        }

        return pdfPath;
    }


    /**
     * convert file to pdf.
     *
     * @param inputStream  the inputstream of a doc file.
     * @param outputStream the outputstream of pdf file.
     */
    public static void converterDocToPdf(InputStream inputStream, OutputStream outputStream) {
        try {
            IConverter converter = LocalConverter.builder().build();
            converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
            // 最后关闭流
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
